using Microsoft.EntityFrameworkCore;
using SheLife.Domain.Common;
using SheLife.Domain.Entities;

namespace SheLife.Infrastructure.Data;

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }

    public DbSet<User> Users { get; set; }
    public DbSet<HealthRecord> HealthRecords { get; set; }
    public DbSet<Habit> Habits { get; set; }
    public DbSet<HabitRecord> HabitRecords { get; set; }
    
    // 生理周期管理
    public DbSet<MenstrualCycle> MenstrualCycles { get; set; }
    public DbSet<MenstrualSymptom> MenstrualSymptoms { get; set; }
    public DbSet<MenstrualNote> MenstrualNotes { get; set; }
    public DbSet<BasalBodyTemperature> BasalBodyTemperatures { get; set; }
    
    // 膳食管理
    public DbSet<DietRecord> DietRecords { get; set; }
    public DbSet<DietNutrient> DietNutrients { get; set; }
    public DbSet<Recipe> Recipes { get; set; }
    public DbSet<RecipeIngredient> RecipeIngredients { get; set; }
    public DbSet<RecipeStep> RecipeSteps { get; set; }
    public DbSet<RecipeNutrient> RecipeNutrients { get; set; }
    public DbSet<UserRecipe> UserRecipes { get; set; }
    
    // 护肤管理
    public DbSet<SkincareRecord> SkincareRecords { get; set; }
    public DbSet<SkincareProduct> SkincareProducts { get; set; }
    public DbSet<SkincareProductRecord> SkincareProductRecords { get; set; }
    public DbSet<UserSkincareProduct> UserSkincareProducts { get; set; }
    public DbSet<SkincareReminder> SkincareReminders { get; set; }
    public DbSet<SkincarePlan> SkincarePlans { get; set; }
    public DbSet<SkincarePlanStep> SkincarePlanSteps { get; set; }
    public DbSet<UserSkincarePlan> UserSkincarePlans { get; set; }
    
    // 穿搭管理
    public DbSet<Clothing> Clothing { get; set; }
    public DbSet<Outfit> Outfits { get; set; }
    public DbSet<OutfitClothing> OutfitClothings { get; set; }
    public DbSet<OutfitRecord> OutfitRecords { get; set; }
    public DbSet<OutfitTemplate> OutfitTemplates { get; set; }
    public DbSet<OutfitTemplateItem> OutfitTemplateItems { get; set; }
    public DbSet<UserOutfitTemplate> UserOutfitTemplates { get; set; }
    public DbSet<OutfitSuggestion> OutfitSuggestions { get; set; }
    
    // 成长管理
    public DbSet<GrowthGoal> GrowthGoals { get; set; }
    public DbSet<GrowthGoalTask> GrowthGoalTasks { get; set; }
    public DbSet<GrowthGoalRecord> GrowthGoalRecords { get; set; }
    public DbSet<GrowthGoalTaskRecord> GrowthGoalTaskRecords { get; set; }
    public DbSet<GrowthArchive> GrowthArchives { get; set; }
    public DbSet<GrowthArchiveItem> GrowthArchiveItems { get; set; }
    public DbSet<EmotionDiary> EmotionDiaries { get; set; }
    public DbSet<EmotionDiaryComment> EmotionDiaryComments { get; set; }
    public DbSet<EmotionDiaryLike> EmotionDiaryLikes { get; set; }
    public DbSet<GrowthCourse> GrowthCourses { get; set; }
    public DbSet<GrowthCourseLesson> GrowthCourseLessons { get; set; }
    public DbSet<UserGrowthCourse> UserGrowthCourses { get; set; }
    public DbSet<UserGrowthCourseProgress> UserGrowthCourseProgresses { get; set; }
    public DbSet<GrowthCourseReview> GrowthCourseReviews { get; set; }
    
    // 社区社交
    public DbSet<CommunityPost> CommunityPosts { get; set; }
    public DbSet<CommunityPostComment> CommunityPostComments { get; set; }
    public DbSet<CommunityPostLike> CommunityPostLikes { get; set; }
    public DbSet<CommunityPostCommentLike> CommunityPostCommentLikes { get; set; }
    public DbSet<CommunityPostShare> CommunityPostShares { get; set; }
    public DbSet<CommunityPostReport> CommunityPostReports { get; set; }
    public DbSet<CommunityPostCommentReport> CommunityPostCommentReports { get; set; }
    public DbSet<UserFriend> UserFriends { get; set; }
    public DbSet<UserFollow> UserFollows { get; set; }
    public DbSet<UserMessage> UserMessages { get; set; }
    public DbSet<UserGroup> UserGroups { get; set; }
    public DbSet<UserGroupMember> UserGroupMembers { get; set; }
    public DbSet<UserGroupMessage> UserGroupMessages { get; set; }
    public DbSet<UserActivity> UserActivities { get; set; }
    public DbSet<UserNotification> UserNotifications { get; set; }
    
    // 系统管理
    public DbSet<SystemConfig> SystemConfigs { get; set; }
    public DbSet<SystemLog> SystemLogs { get; set; }
    public DbSet<SystemTask> SystemTasks { get; set; }
    public DbSet<SystemTaskLog> SystemTaskLogs { get; set; }
    public DbSet<SystemNotification> SystemNotifications { get; set; }
    public DbSet<SystemNotificationUser> SystemNotificationUsers { get; set; }
    public DbSet<SystemVersion> SystemVersions { get; set; }
    public DbSet<SystemFeedback> SystemFeedbacks { get; set; }
    public DbSet<SystemDictionary> SystemDictionaries { get; set; }
    public DbSet<SystemRole> SystemRoles { get; set; }
    public DbSet<SystemPermission> SystemPermissions { get; set; }
    public DbSet<SystemRolePermission> SystemRolePermissions { get; set; }
    public DbSet<SystemUserRole> SystemUserRoles { get; set; }
    public DbSet<SystemOperationLog> SystemOperationLogs { get; set; }
    public DbSet<SystemLoginLog> SystemLoginLogs { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // 配置实体
        modelBuilder.Entity<User>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Username).IsRequired().HasMaxLength(50);
            entity.Property(e => e.Email).IsRequired().HasMaxLength(100);
            entity.Property(e => e.PasswordHash).IsRequired();
            entity.Property(e => e.PhoneNumber).HasMaxLength(20);
            entity.Property(e => e.Gender).HasMaxLength(10);
            entity.Property(e => e.Avatar).HasMaxLength(200);
            
            // 唯一索引
            entity.HasIndex(e => e.Username).IsUnique();
            entity.HasIndex(e => e.Email).IsUnique();
        });

        modelBuilder.Entity<HealthRecord>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.RecordType).IsRequired().HasMaxLength(50);
            entity.Property(e => e.Title).IsRequired().HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Unit).HasMaxLength(20);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany(e => e.HealthRecords)
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<Habit>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Title).IsRequired().HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Category).IsRequired().HasMaxLength(50);
            entity.Property(e => e.Icon).HasMaxLength(50);
            entity.Property(e => e.Color).HasMaxLength(20);
            entity.Property(e => e.Frequency).HasMaxLength(20);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany(e => e.Habits)
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<HabitRecord>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Note).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany(e => e.HabitRecords)
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联习惯
            entity.HasOne(e => e.Habit)
                  .WithMany(e => e.HabitRecords)
                  .HasForeignKey(e => e.HabitId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 生理周期管理实体配置
        modelBuilder.Entity<MenstrualCycle>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.CycleType).HasMaxLength(20);
            entity.Property(e => e.Status).HasMaxLength(20);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<MenstrualSymptom>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.SymptomType).HasMaxLength(50);
            entity.Property(e => e.Severity).HasMaxLength(20);
            
            // 关联生理周期
            entity.HasOne(e => e.MenstrualCycle)
                  .WithMany(e => e.MenstrualSymptoms)
                  .HasForeignKey(e => e.MenstrualCycleId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<MenstrualNote>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Title).HasMaxLength(100);
            entity.Property(e => e.Content).HasMaxLength(1000);
            
            // 关联生理周期
            entity.HasOne(e => e.MenstrualCycle)
                  .WithMany(e => e.MenstrualNotes)
                  .HasForeignKey(e => e.MenstrualCycleId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<BasalBodyTemperature>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Note).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 膳食管理实体配置
        modelBuilder.Entity<DietRecord>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.MealType).HasMaxLength(20);
            entity.Property(e => e.FoodName).HasMaxLength(100);
            entity.Property(e => e.Note).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<DietNutrient>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.NutrientName).HasMaxLength(50);
            entity.Property(e => e.Unit).HasMaxLength(20);
            
            // 关联膳食记录
            entity.HasOne(e => e.DietRecord)
                  .WithMany(e => e.DietNutrients)
                  .HasForeignKey(e => e.DietRecordId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<Recipe>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.RecipeName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.Difficulty).HasMaxLength(20);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            
            // 唯一索引
            entity.HasIndex(e => e.RecipeName).IsUnique();
        });

        modelBuilder.Entity<RecipeIngredient>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.IngredientName).HasMaxLength(100);
            entity.Property(e => e.Unit).HasMaxLength(20);
            
            // 关联食谱
            entity.HasOne(e => e.Recipe)
                  .WithMany(e => e.RecipeIngredients)
                  .HasForeignKey(e => e.RecipeId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<RecipeStep>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.StepDescription).HasMaxLength(1000);
            
            // 关联食谱
            entity.HasOne(e => e.Recipe)
                  .WithMany(e => e.RecipeSteps)
                  .HasForeignKey(e => e.RecipeId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<RecipeNutrient>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.NutrientName).HasMaxLength(50);
            entity.Property(e => e.Unit).HasMaxLength(20);
            
            // 关联食谱
            entity.HasOne(e => e.Recipe)
                  .WithMany(e => e.RecipeNutrients)
                  .HasForeignKey(e => e.RecipeId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserRecipe>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联食谱
            entity.HasOne(e => e.Recipe)
                  .WithMany()
                  .HasForeignKey(e => e.RecipeId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 护肤管理实体配置
        modelBuilder.Entity<SkincareRecord>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.SkinCondition).HasMaxLength(50);
            entity.Property(e => e.Note).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<SkincareProduct>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.ProductName).HasMaxLength(100);
            entity.Property(e => e.Brand).HasMaxLength(50);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.SubCategory).HasMaxLength(50);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            
            // 唯一索引
            entity.HasIndex(e => e.ProductName).IsUnique();
        });

        modelBuilder.Entity<SkincareProductRecord>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.UsageAmount).HasMaxLength(20);
            entity.Property(e => e.Note).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联护肤产品
            entity.HasOne(e => e.SkincareProduct)
                  .WithMany()
                  .HasForeignKey(e => e.SkincareProductId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserSkincareProduct>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.PurchaseLocation).HasMaxLength(100);
            entity.Property(e => e.StorageLocation).HasMaxLength(50);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联护肤产品
            entity.HasOne(e => e.SkincareProduct)
                  .WithMany()
                  .HasForeignKey(e => e.SkincareProductId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<SkincareReminder>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.ReminderType).HasMaxLength(20);
            entity.Property(e => e.Title).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<SkincarePlan>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.PlanName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.SkinType).HasMaxLength(20);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            
            // 唯一索引
            entity.HasIndex(e => e.PlanName).IsUnique();
        });

        modelBuilder.Entity<SkincarePlanStep>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.StepName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.ProductName).HasMaxLength(100);
            
            // 关联护肤计划
            entity.HasOne(e => e.SkincarePlan)
                  .WithMany(e => e.SkincarePlanSteps)
                  .HasForeignKey(e => e.SkincarePlanId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserSkincarePlan>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联护肤计划
            entity.HasOne(e => e.SkincarePlan)
                  .WithMany()
                  .HasForeignKey(e => e.SkincarePlanId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 穿搭管理实体配置
        modelBuilder.Entity<Clothing>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.ClothingName).HasMaxLength(100);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.SubCategory).HasMaxLength(50);
            entity.Property(e => e.Brand).HasMaxLength(50);
            entity.Property(e => e.Color).HasMaxLength(20);
            entity.Property(e => e.Size).HasMaxLength(20);
            entity.Property(e => e.Material).HasMaxLength(50);
            entity.Property(e => e.Style).HasMaxLength(50);
            entity.Property(e => e.Season).HasMaxLength(20);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            entity.Property(e => e.StorageLocation).HasMaxLength(50);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<Outfit>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.OutfitName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Occasion).HasMaxLength(50);
            entity.Property(e => e.Season).HasMaxLength(20);
            entity.Property(e => e.Style).HasMaxLength(50);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<OutfitClothing>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Position).HasMaxLength(20);
            
            // 关联穿搭
            entity.HasOne(e => e.Outfit)
                  .WithMany(e => e.OutfitClothings)
                  .HasForeignKey(e => e.OutfitId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联衣物
            entity.HasOne(e => e.Clothing)
                  .WithMany()
                  .HasForeignKey(e => e.ClothingId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<OutfitRecord>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Occasion).HasMaxLength(50);
            entity.Property(e => e.Weather).HasMaxLength(50);
            entity.Property(e => e.Mood).HasMaxLength(20);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联穿搭
            entity.HasOne(e => e.Outfit)
                  .WithMany(e => e.OutfitRecords)
                  .HasForeignKey(e => e.OutfitId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<OutfitTemplate>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.TemplateName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Occasion).HasMaxLength(50);
            entity.Property(e => e.Season).HasMaxLength(20);
            entity.Property(e => e.Style).HasMaxLength(50);
            entity.Property(e => e.LifeStage).HasMaxLength(50);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            
            // 唯一索引
            entity.HasIndex(e => e.TemplateName).IsUnique();
        });

        modelBuilder.Entity<OutfitTemplateItem>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.ItemName).HasMaxLength(100);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.SubCategory).HasMaxLength(50);
            entity.Property(e => e.Color).HasMaxLength(20);
            entity.Property(e => e.Style).HasMaxLength(50);
            entity.Property(e => e.Material).HasMaxLength(50);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Tips).HasMaxLength(500);
            
            // 关联穿搭模板
            entity.HasOne(e => e.OutfitTemplate)
                  .WithMany(e => e.OutfitTemplateItems)
                  .HasForeignKey(e => e.OutfitTemplateId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserOutfitTemplate>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联穿搭模板
            entity.HasOne(e => e.OutfitTemplate)
                  .WithMany()
                  .HasForeignKey(e => e.OutfitTemplateId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<OutfitSuggestion>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.SuggestionTitle).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Occasion).HasMaxLength(50);
            entity.Property(e => e.Season).HasMaxLength(20);
            entity.Property(e => e.Weather).HasMaxLength(50);
            entity.Property(e => e.LifeStage).HasMaxLength(50);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 成长管理实体配置
        modelBuilder.Entity<GrowthGoal>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.GoalName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.Unit).HasMaxLength(20);
            entity.Property(e => e.Status).HasMaxLength(20);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<GrowthGoalTask>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.TaskName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Unit).HasMaxLength(20);
            entity.Property(e => e.Frequency).HasMaxLength(20);
            entity.Property(e => e.Status).HasMaxLength(20);
            
            // 关联成长目标
            entity.HasOne(e => e.GrowthGoal)
                  .WithMany(e => e.GrowthGoalTasks)
                  .HasForeignKey(e => e.GoalId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<GrowthGoalRecord>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Notes).HasMaxLength(500);
            entity.Property(e => e.Mood).HasMaxLength(20);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            
            // 关联成长目标
            entity.HasOne(e => e.GrowthGoal)
                  .WithMany(e => e.GrowthGoalRecords)
                  .HasForeignKey(e => e.GoalId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<GrowthGoalTaskRecord>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Notes).HasMaxLength(500);
            entity.Property(e => e.Mood).HasMaxLength(20);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            
            // 关联成长目标任务
            entity.HasOne(e => e.GrowthGoalTask)
                  .WithMany(e => e.GrowthGoalTaskRecords)
                  .HasForeignKey(e => e.TaskId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联成长目标
            entity.HasOne(e => e.GrowthGoal)
                  .WithMany()
                  .HasForeignKey(e => e.GoalId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<GrowthArchive>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.ArchiveName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.CoverImage).HasMaxLength(200);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<GrowthArchiveItem>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.ItemType).HasMaxLength(50);
            entity.Property(e => e.ItemName).HasMaxLength(100);
            entity.Property(e => e.ItemDescription).HasMaxLength(500);
            entity.Property(e => e.ItemImageUrl).HasMaxLength(200);
            
            // 关联成长档案
            entity.HasOne(e => e.GrowthArchive)
                  .WithMany(e => e.GrowthArchiveItems)
                  .HasForeignKey(e => e.ArchiveId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<EmotionDiary>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Title).HasMaxLength(100);
            entity.Property(e => e.Content).HasMaxLength(2000);
            entity.Property(e => e.Mood).HasMaxLength(20);
            entity.Property(e => e.EmotionTags).HasMaxLength(200);
            entity.Property(e => e.ImageUrls).HasMaxLength(1000);
            entity.Property(e => e.Tags).HasMaxLength(200);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<EmotionDiaryComment>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联情绪日记
            entity.HasOne(e => e.EmotionDiary)
                  .WithMany(e => e.EmotionDiaryComments)
                  .HasForeignKey(e => e.DiaryId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 自引用 - 父评论
            entity.HasOne(e => e.ParentComment)
                  .WithMany(e => e.ChildComments)
                  .HasForeignKey(e => e.ParentCommentId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        modelBuilder.Entity<EmotionDiaryLike>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联情绪日记
            entity.HasOne(e => e.EmotionDiary)
                  .WithMany(e => e.EmotionDiaryLikes)
                  .HasForeignKey(e => e.DiaryId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<GrowthCourse>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.CourseName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.Instructor).HasMaxLength(50);
            entity.Property(e => e.CoverImage).HasMaxLength(200);
            entity.Property(e => e.VideoUrl).HasMaxLength(200);
            entity.Property(e => e.Tags).HasMaxLength(200);
            
            // 唯一索引
            entity.HasIndex(e => e.CourseName).IsUnique();
        });

        modelBuilder.Entity<GrowthCourseLesson>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.LessonName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.VideoUrl).HasMaxLength(200);
            entity.Property(e => e.Notes).HasMaxLength(1000);
            
            // 关联成长课程
            entity.HasOne(e => e.GrowthCourse)
                  .WithMany(e => e.GrowthCourseLessons)
                  .HasForeignKey(e => e.CourseId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserGrowthCourse>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Status).HasMaxLength(20);
            entity.Property(e => e.Review).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联成长课程
            entity.HasOne(e => e.GrowthCourse)
                  .WithMany()
                  .HasForeignKey(e => e.CourseId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserGrowthCourseProgress>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联成长课程
            entity.HasOne(e => e.GrowthCourse)
                  .WithMany()
                  .HasForeignKey(e => e.CourseId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联成长课程课时
            entity.HasOne(e => e.GrowthCourseLesson)
                  .WithMany()
                  .HasForeignKey(e => e.LessonId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<GrowthCourseReview>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Content).HasMaxLength(500);
            
            // 关联成长课程
            entity.HasOne(e => e.GrowthCourse)
                  .WithMany(e => e.GrowthCourseReviews)
                  .HasForeignKey(e => e.CourseId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 社区社交实体配置
        modelBuilder.Entity<CommunityPost>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Title).HasMaxLength(100);
            entity.Property(e => e.Content).HasMaxLength(2000);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.SubCategory).HasMaxLength(50);
            entity.Property(e => e.Tags).HasMaxLength(200);
            entity.Property(e => e.ImageUrls).HasMaxLength(1000);
            entity.Property(e => e.VideoUrl).HasMaxLength(200);
            entity.Property(e => e.Status).HasMaxLength(20);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<CommunityPostComment>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Content).HasMaxLength(1000);
            entity.Property(e => e.ImageUrls).HasMaxLength(1000);
            entity.Property(e => e.Status).HasMaxLength(20);
            
            // 关联社区帖子
            entity.HasOne(e => e.CommunityPost)
                  .WithMany(e => e.CommunityPostComments)
                  .HasForeignKey(e => e.PostId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 自引用 - 父评论
            entity.HasOne(e => e.ParentComment)
                  .WithMany(e => e.ChildComments)
                  .HasForeignKey(e => e.ParentCommentId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        modelBuilder.Entity<CommunityPostLike>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联社区帖子
            entity.HasOne(e => e.CommunityPost)
                  .WithMany(e => e.CommunityPostLikes)
                  .HasForeignKey(e => e.PostId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<CommunityPostCommentLike>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联社区帖子评论
            entity.HasOne(e => e.CommunityPostComment)
                  .WithMany(e => e.CommunityPostCommentLikes)
                  .HasForeignKey(e => e.CommentId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<CommunityPostShare>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.SharePlatform).HasMaxLength(20);
            entity.Property(e => e.ShareUrl).HasMaxLength(200);
            
            // 关联社区帖子
            entity.HasOne(e => e.CommunityPost)
                  .WithMany(e => e.CommunityPostShares)
                  .HasForeignKey(e => e.PostId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<CommunityPostReport>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Reason).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Status).HasMaxLength(20);
            entity.Property(e => e.HandleResult).HasMaxLength(500);
            
            // 关联社区帖子
            entity.HasOne(e => e.CommunityPost)
                  .WithMany(e => e.CommunityPostReports)
                  .HasForeignKey(e => e.PostId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联处理人
            entity.HasOne(e => e.HandleUser)
                  .WithMany()
                  .HasForeignKey(e => e.HandleUserId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        modelBuilder.Entity<CommunityPostCommentReport>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Reason).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Status).HasMaxLength(20);
            entity.Property(e => e.HandleResult).HasMaxLength(500);
            
            // 关联社区帖子评论
            entity.HasOne(e => e.CommunityPostComment)
                  .WithMany(e => e.CommunityPostCommentReports)
                  .HasForeignKey(e => e.CommentId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联处理人
            entity.HasOne(e => e.HandleUser)
                  .WithMany()
                  .HasForeignKey(e => e.HandleUserId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        modelBuilder.Entity<UserFriend>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Status).HasMaxLength(20);
            entity.Property(e => e.Remark).HasMaxLength(100);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联好友
            entity.HasOne(e => e.Friend)
                  .WithMany()
                  .HasForeignKey(e => e.FriendId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserFollow>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联被关注用户
            entity.HasOne(e => e.FollowedUser)
                  .WithMany()
                  .HasForeignKey(e => e.FollowedUserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserMessage>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.MessageType).HasMaxLength(20);
            entity.Property(e => e.MediaUrl).HasMaxLength(200);
            entity.Property(e => e.Status).HasMaxLength(20);
            
            // 关联发送者
            entity.HasOne(e => e.Sender)
                  .WithMany()
                  .HasForeignKey(e => e.SenderId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联接收者
            entity.HasOne(e => e.Receiver)
                  .WithMany()
                  .HasForeignKey(e => e.ReceiverId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserGroup>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.GroupName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Avatar).HasMaxLength(200);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.JoinRule).HasMaxLength(20);
            entity.Property(e => e.Tags).HasMaxLength(200);
            
            // 关联创建者
            entity.HasOne(e => e.Creator)
                  .WithMany()
                  .HasForeignKey(e => e.CreatorId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserGroupMember>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Role).HasMaxLength(20);
            entity.Property(e => e.Nickname).HasMaxLength(50);
            entity.Property(e => e.Status).HasMaxLength(20);
            
            // 关联用户群组
            entity.HasOne(e => e.UserGroup)
                  .WithMany(e => e.UserGroupMembers)
                  .HasForeignKey(e => e.GroupId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserGroupMessage>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.MessageType).HasMaxLength(20);
            entity.Property(e => e.MediaUrl).HasMaxLength(200);
            entity.Property(e => e.Status).HasMaxLength(20);
            
            // 关联用户群组
            entity.HasOne(e => e.UserGroup)
                  .WithMany(e => e.UserGroupMessages)
                  .HasForeignKey(e => e.GroupId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联发送者
            entity.HasOne(e => e.Sender)
                  .WithMany()
                  .HasForeignKey(e => e.SenderId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserActivity>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.ActivityType).HasMaxLength(50);
            entity.Property(e => e.ActivityContent).HasMaxLength(500);
            entity.Property(e => e.TargetType).HasMaxLength(50);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<UserNotification>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.NotificationType).HasMaxLength(50);
            entity.Property(e => e.Title).HasMaxLength(100);
            entity.Property(e => e.Content).HasMaxLength(500);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            entity.Property(e => e.ActionUrl).HasMaxLength(200);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // 系统管理实体配置
        modelBuilder.Entity<SystemConfig>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.ConfigKey).HasMaxLength(100);
            entity.Property(e => e.ConfigValue).HasMaxLength(500);
            entity.Property(e => e.Description).HasMaxLength(200);
            entity.Property(e => e.Category).HasMaxLength(50);
            
            // 唯一索引
            entity.HasIndex(e => e.ConfigKey).IsUnique();
        });

        modelBuilder.Entity<SystemLog>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.LogLevel).HasMaxLength(20);
            entity.Property(e => e.Category).HasMaxLength(50);
            entity.Property(e => e.Message).HasMaxLength(2000);
            entity.Property(e => e.RequestUrl).HasMaxLength(200);
            entity.Property(e => e.RequestMethod).HasMaxLength(20);
            entity.Property(e => e.RequestParameters).HasMaxLength(1000);
            entity.Property(e => e.RequestIp).HasMaxLength(50);
            entity.Property(e => e.UserAgent).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        modelBuilder.Entity<SystemTask>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.TaskName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.TaskType).HasMaxLength(50);
            entity.Property(e => e.CronExpression).HasMaxLength(100);
            entity.Property(e => e.TaskHandler).HasMaxLength(200);
            entity.Property(e => e.TaskParameters).HasMaxLength(1000);
            entity.Property(e => e.Status).HasMaxLength(20);
            entity.Property(e => e.LastFailureMessage).HasMaxLength(500);
        });

        modelBuilder.Entity<SystemTaskLog>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.TaskName).HasMaxLength(100);
            entity.Property(e => e.Status).HasMaxLength(20);
            entity.Property(e => e.Message).HasMaxLength(1000);
            entity.Property(e => e.Exception).HasMaxLength(2000);
            
            // 关联系统任务
            entity.HasOne(e => e.SystemTask)
                  .WithMany(e => e.SystemTaskLogs)
                  .HasForeignKey(e => e.TaskId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<SystemNotification>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Title).HasMaxLength(100);
            entity.Property(e => e.Content).HasMaxLength(500);
            entity.Property(e => e.NotificationType).HasMaxLength(50);
            entity.Property(e => e.TargetType).HasMaxLength(50);
            entity.Property(e => e.TargetIds).HasMaxLength(1000);
            entity.Property(e => e.ImageUrl).HasMaxLength(200);
            entity.Property(e => e.ActionUrl).HasMaxLength(200);
            entity.Property(e => e.Status).HasMaxLength(20);
        });

        modelBuilder.Entity<SystemNotificationUser>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联系统通知
            entity.HasOne(e => e.SystemNotification)
                  .WithMany(e => e.SystemNotificationUsers)
                  .HasForeignKey(e => e.NotificationId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<SystemVersion>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Version).HasMaxLength(50);
            entity.Property(e => e.VersionName).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.Platform).HasMaxLength(20);
            entity.Property(e => e.DownloadUrl).HasMaxLength(200);
            entity.Property(e => e.UpdateType).HasMaxLength(20);
            entity.Property(e => e.UpdateContent).HasMaxLength(1000);
            
            // 唯一索引
            entity.HasIndex(e => new { e.Version, e.Platform }).IsUnique();
        });

        modelBuilder.Entity<SystemFeedback>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.FeedbackType).HasMaxLength(50);
            entity.Property(e => e.Title).HasMaxLength(100);
            entity.Property(e => e.Content).HasMaxLength(1000);
            entity.Property(e => e.Contact).HasMaxLength(100);
            entity.Property(e => e.ImageUrls).HasMaxLength(1000);
            entity.Property(e => e.Status).HasMaxLength(20);
            entity.Property(e => e.HandleResult).HasMaxLength(500);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Restrict);
            
            // 关联处理人
            entity.HasOne(e => e.HandleUser)
                  .WithMany()
                  .HasForeignKey(e => e.HandleUserId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        modelBuilder.Entity<SystemDictionary>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.DictionaryType).HasMaxLength(50);
            entity.Property(e => e.DictionaryKey).HasMaxLength(100);
            entity.Property(e => e.DictionaryValue).HasMaxLength(200);
            entity.Property(e => e.Description).HasMaxLength(200);
            
            // 唯一索引
            entity.HasIndex(e => new { e.DictionaryType, e.DictionaryKey }).IsUnique();
        });

        modelBuilder.Entity<SystemRole>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.RoleName).HasMaxLength(50);
            entity.Property(e => e.RoleCode).HasMaxLength(50);
            entity.Property(e => e.Description).HasMaxLength(200);
            
            // 唯一索引
            entity.HasIndex(e => e.RoleName).IsUnique();
            entity.HasIndex(e => e.RoleCode).IsUnique();
        });

        modelBuilder.Entity<SystemPermission>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.PermissionName).HasMaxLength(100);
            entity.Property(e => e.PermissionCode).HasMaxLength(100);
            entity.Property(e => e.Description).HasMaxLength(200);
            entity.Property(e => e.PermissionType).HasMaxLength(20);
            entity.Property(e => e.Path).HasMaxLength(200);
            entity.Property(e => e.Icon).HasMaxLength(100);
            entity.Property(e => e.Component).HasMaxLength(200);
            
            // 唯一索引
            entity.HasIndex(e => e.PermissionCode).IsUnique();
            
            // 自引用 - 父权限
            entity.HasOne(e => e.Parent)
                  .WithMany(e => e.Children)
                  .HasForeignKey(e => e.ParentId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        modelBuilder.Entity<SystemRolePermission>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联系统角色
            entity.HasOne(e => e.SystemRole)
                  .WithMany(e => e.SystemRolePermissions)
                  .HasForeignKey(e => e.RoleId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联系统权限
            entity.HasOne(e => e.SystemPermission)
                  .WithMany()
                  .HasForeignKey(e => e.PermissionId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<SystemUserRole>(entity =>
        {
            entity.HasKey(e => e.Id);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            // 关联系统角色
            entity.HasOne(e => e.SystemRole)
                  .WithMany()
                  .HasForeignKey(e => e.RoleId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<SystemOperationLog>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.OperationType).HasMaxLength(50);
            entity.Property(e => e.OperationModule).HasMaxLength(50);
            entity.Property(e => e.OperationContent).HasMaxLength(500);
            entity.Property(e => e.OperationResult).HasMaxLength(20);
            entity.Property(e => e.RequestUrl).HasMaxLength(200);
            entity.Property(e => e.RequestMethod).HasMaxLength(20);
            entity.Property(e => e.RequestParameters).HasMaxLength(1000);
            entity.Property(e => e.RequestIp).HasMaxLength(50);
            entity.Property(e => e.UserAgent).HasMaxLength(500);
            entity.Property(e => e.Exception).HasMaxLength(2000);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        modelBuilder.Entity<SystemLoginLog>(entity =>
        {
            entity.HasKey(e => e.Id);
            entity.Property(e => e.LoginType).HasMaxLength(20);
            entity.Property(e => e.LoginResult).HasMaxLength(20);
            entity.Property(e => e.FailureReason).HasMaxLength(200);
            entity.Property(e => e.LoginIp).HasMaxLength(50);
            entity.Property(e => e.LoginLocation).HasMaxLength(100);
            entity.Property(e => e.Device).HasMaxLength(100);
            entity.Property(e => e.Browser).HasMaxLength(100);
            entity.Property(e => e.OperatingSystem).HasMaxLength(100);
            
            // 关联用户
            entity.HasOne(e => e.User)
                  .WithMany()
                  .HasForeignKey(e => e.UserId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        // 全局查询过滤器 - 软删除
        foreach (var entityType in modelBuilder.Model.GetEntityTypes())
        {
            if (typeof(BaseEntity).IsAssignableFrom(entityType.ClrType))
            {
                modelBuilder.Entity(entityType.ClrType)
                    .HasQueryFilter(GenerateSoftDeleteFilter(entityType.ClrType));
            }
        }
    }

    private static LambdaExpression GenerateSoftDeleteFilter(Type entityType)
    {
        var parameter = Expression.Parameter(entityType, "e");
        var property = Expression.Property(parameter, nameof(BaseEntity.IsDeleted));
        var condition = Expression.Equal(property, Expression.Constant(false));
        return Expression.Lambda(condition, parameter);
    }

    public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        foreach (var entry in ChangeTracker.Entries<BaseEntity>())
        {
            switch (entry.State)
            {
                case EntityState.Added:
                    entry.Entity.CreatedAt = DateTime.UtcNow;
                    break;
                case EntityState.Modified:
                    entry.Entity.UpdatedAt = DateTime.UtcNow;
                    break;
            }
        }

        return base.SaveChangesAsync(cancellationToken);
    }
}